% ML TAU 2013 final project script
% Alt 6:
% Perform 

%base_path = '/home/itay/TAU/IML/final';
%libsvm_path = '/opt/libsvm-3.17/matlab';

% Add path to the libsvm
%addpath(base_path);
%addpath(libsvm_path);

%
% Initialization
%
close all; clear; clc; tic;
if ~exist('dataforproject.mat','file')
    error('Data file not found in current directory')
end;
% Saving memory. Loading vars on a need to load basis only throughout
load('dataforproject.mat','X1train','X2train','gidtrain','ytrain');
fprintf('Training Data successfully loaded\n');

num_pc_to_skip_attempts = 10;
num_pc_to_use_attempts = 20;

PC_Vec = floor(linspace(10, 6222, num_pc_to_use_attempts));

Results = zeros(num_pc_to_skip_attempts, num_pc_to_use_attempts);

for num_pc_to_skip = 1:num_pc_to_skip_attempts
for pc_to_use_idx = 1:num_pc_to_use_attempts

CV_Results = zeros(3, 1);

for k=1:3
	
	num_pc_to_use = PC_Vec(pc_to_use_idx);
	fprintf('PC to skip: %f To use: %f\n', num_pc_to_skip, num_pc_to_use);

	% Training set
	X1 = X1train(:,(gidtrain~=k));
	X2 = X2train(:,(gidtrain~=k));
	y = ytrain(gidtrain~=k);
	
	% Simply sum the deltas between the vectors 
	DiffX = (X1 - X2);
	[ EigenVectors, MeanX ] = PerformPCA(DiffX, [ 'X1train-X2train_', int2str(k) ]);
	TopEigenVectors = EigenVectors(:, num_pc_to_skip:num_pc_to_use);
	TransformedX = ((DiffX - repmat(MeanX, 1, size(DiffX, 2)))' * TopEigenVectors)';
	%TransformedX = (DiffX' * EigenVectors)';
	X = sum(abs(TransformedX))';

	% Create the model
	model = LinearModel.fit(X,y,'linear');

	% Testing set
	X1 = X1train(:,(gidtrain==k));
	X2 = X2train(:,(gidtrain==k));
	y = ytrain(gidtrain==k);
	
	DiffX = (X1 - X2);
	TransformedX = ((DiffX - repmat(MeanX, 1, size(DiffX, 2)))' * TopEigenVectors)';
	%TransformedX = (DiffX' * EigenVectors)';
	X = sum(abs(TransformedX))';

	% Predict
	predictedY = predict(model, X);
 
	CV_Results(k) = sum(y.*predictedY > 0) / size(y,1); 

	fprintf('Results with PC=%f..%f: %f\n', num_pc_to_skip, num_pc_to_use, CV_Results(k));
end


Results(num_pc_to_skip, pc_to_use_idx) = mean(CV_Results);
fprintf('Average results with PC=%f..%f: %f\n', num_pc_to_skip, num_pc_to_use, Results(num_pc_to_skip, pc_to_use_idx));

end
end

[ best_results, best_results_idx ] = max(Results);
fprintf('Best results: %f\n', best_results);
disp(best_results_idx);


